$("#changeCode").on("click", function () {
    changeCode();
})

function changeCode() {
    $("#changeCode").attr("src", "ValidateCode.aspx?code=" + (new Date()).getTime());
}


$("#adminForm").validate({
    rules: {
        user: {
            required: true
        },
        pwd: {
            required: true
        }
    },
    messages: {
        user: {
            required: "请输入用户名"
        },
        pwd: {
            required: "请输入密码"
        }
    },

    errorClass: "validate-tip",
    errorElement: "label",
    errorPlacement: function (error, element) {
        $(element)
            .parent()
            .append(error);
    },
    submitHandler: function (form) {
        var e = event || window.event;
        e.preventDefault();
        var data = $(form).serializeArray();
        console.log(form.user.value);

        // 1 先获取文本框的数据
        var username = form.user.value;
        var password = form.pwd.value;
        var vericode = form.code.value;

        // 2 组装好送往后台的json数据
        var _json = jQuery.param({"username": username, "password": password, "vericode": vericode, "type": "login"});

        // 3 指定送向后台哪个处理程序以及传送的方式，并传送
        var request = $.ajax({
            url: "../api/login.ashx",
            type: "POST",
            async: false,
            data: _json, //不能直接写成 {id:"123",code:"tomcat"}  
            dataType: "json",
            // contentType: "charset=utf-8",  
            cache: false,

            //4 接受返回过来的数据，同样是json格式的，根据返回过来的数据给出相应的提示或操作
            success: function (data) {
                var json = data;

                if (json.r > 0) {
                    //提示插入成功
                    //alert(json.data);   
                    window.location.href = "./movie.aspx"
                    //关闭弹出窗口
                } else {
                    alert(json.data);
                    changeCode();
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.readyState);
            }
        });


    }
});

//function getCode () {
// var _json = jQuery.param({ "type": "init" });  
//        var request = $.ajax({  
//            url: "../api/login.ashx",  
//            type: "POST",  
//            async: false,  
//            data: _json, //不能直接写成 {id:"123",code:"tomcat"}  
//            dataType: "json",  
//            // contentType: "charset=utf-8",  
//            cache: false,  
//            
//            success: function (data) {  
//                var json=data;
//                $("#code").html(data)
//            },  
//            error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.readyState); }  
//        }); 
//}

//getCode ();